using System;
using UnityEngine;
using WooEngine.UI.Config;
using WooEngine.UI.Interfaces;

namespace WooEngine.UI.Base
{

    public abstract class AbstractUIPage<T> : AbstractUIComponent<T>, IUIPage
    {

        public PageStatusType PageStatus { get; private set; }

        public PageModeType PageMode { get; private set; }

        public PageLayerType PageLayer { get; private set; }

        public int SiblingIndex { get; private set; }

        public string PageName => m_name;

        protected Canvas m_canvas;

        public override void Init(string name, GameObject viewGO, object userData)
        {
            base.Init(name, viewGO, userData);
            m_canvas = viewGO.GetComponent<Canvas>();
        }

        public virtual void FadeIn(Action<string> OnFadeInFinish = null)
        {
            this.OnEnter();
            void OnFadeInAnimFinish(string _pageName)
            {
                this.OnPageShow();
                if (OnFadeInFinish != null)
                {
                    OnFadeInFinish(_pageName);
                }
                this.OnPageShowOver();
            }
            this.FadeInAnim(OnFadeInAnimFinish);
        }

        public virtual void FadeInAnim(Action<string> OnFadeInAnimFinish = null)
        {
            if(OnFadeInAnimFinish != null)
            {
                OnFadeInAnimFinish(m_name);
            }
        }

        public virtual void FadeOut(Action<string> OnFadeOutFinish = null)
        {
            void OnFadeOutAnimFinish(string _pageName)
            {
                this.OnExit();
                if (OnFadeOutFinish != null)
                {
                    OnFadeOutFinish(_pageName);
                }
            }
            this.FadeOutAnim(OnFadeOutAnimFinish);
        }

        public virtual void FadeOutAnim(Action<string> OnFadeOutAnimFinish = null)
        {
            if (OnFadeOutAnimFinish != null)
            {
                OnFadeOutAnimFinish(m_name);
            }
        }

        public virtual void OnAndroidBackBtnClick()
        {

        }

        public virtual void OnBackGroundClick()
        {

        }

        public virtual void OnEnter()
        {
            m_go.SetActive(true);
        }

        public virtual void OnExit()
        {

        }

        public virtual void OnPageShow()
        {

        }

        public virtual void OnPageShowOver()
        {

        }

        public virtual void Release()
        {

        }

        public abstract void TouchSwitch(bool isOn);

        public void SetPageLayer(PageLayerType pageLayer)
        {
            PageLayer = pageLayer;
        }

        public void SetPageMode(PageModeType pageMode)
        {
            PageMode = pageMode;
        }

        public void SetPageStatus(PageStatusType pageStatus)
        {
            PageStatusType pageStatus_old = PageStatus;
            PageStatus = pageStatus;

            if (pageStatus_old >= PageStatusType.Inited)
            {
                TouchSwitch(pageStatus == PageStatusType.Opened);
            }
        }

        public void SetSiblingIndex(int index)
        {
            SiblingIndex = index;
            ViewGO.transform.SetSiblingIndex(index);
            if (m_canvas != null)
            {
                int sortingOrder_old = m_canvas.sortingOrder;
                m_canvas.sortingOrder = index * 100;
            }
        }
    }

}